﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Service;

namespace community
{
    
    public partial class Create : Form
    {
        residentcheck re = new residentcheck();
        string cr_JMBH;
        //public static string state = Createstate;
        public Create()
        {
            InitializeComponent();
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            re.ShowDialog();
            
            cr_JMBH = residentcheck.resident_JMBH.Trim();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sql = "select inhabitantcode_chr as 居民编号,residentname_chr as 居民姓名,identitycard_chr as 身份证号,sex_chr as 性别,birth_dat as 出生日期, communityname_chr as 所属社区,communitycode_chr as 社区编号,familycode_chr as 家庭编号,company_chr as 工作单位,culture_vchr as 文化程度,phone_chr as 联系电话,addressprovince_vchr as 家庭地址_省,addresscity_vchr as 家庭地址_市,addresscounty_vchr as 家庭地址_县,patientcardid_chr as 诊疗卡号,careerstyle_chr as 职业 from  [community].[dbo].[T_CS_INHABITANTRECORD_NEW] where inhabitantcode_chr='" + cr_JMBH + "'";
            string sql1 = "select ZGZL_VCHR as 主观资料,KGZL_VCHR as 客观资料,PG_VCHR as 评估,CZJH_VCHR as 处置计划,CREATEDATE_DAT as 接诊时间,CREATEDOCTORMANE_VCHR as 医生签名 from T_CS_RECEIVEEXAMINERECORD where inhabitantcode_chr='" + cr_JMBH + "'";
            Cls_DBHelper help = new Cls_DBHelper();
            SqlDataReader dr = help.m_clsGetReader(sql);
            Cls_DBHelper help1 = new Cls_DBHelper();
            SqlDataReader dr1 = help1.m_clsGetReader(sql1);
            //SqlCommand dr = new SqlCommand(sql, cn);

            //textBox1.Text = dr1.GetValue(0).ToString();
            //textBox2.Text = dr1.GetValue().ToString();
            //textBox3.Text = dr1.GetValue().ToString();
            //textBox4.Text = dr1.GetValue().ToString();
            //textBox5.Text = dr1.GetValue().ToString();
            //textBox6.Text = dr1.GetValue().ToString();
            //textBox7.Text = dr1.GetValue().ToString();
            //textBox8.Text = dr1.GetValue().ToString();
            //textBox9.Text = dr1.GetValue().ToString();
            //textBox10.Text = dr1.GetValue().ToString();
            //textBox11.Text = dr1.GetValue().ToString();
            //textBox12.Text = dr1.GetValue().ToString();

            if (dr.Read())
            {
                string str1 = dr["家庭地址_省"].ToString();
                string str2 = dr["家庭地址_市"].ToString();
                string str3 = dr["家庭地址_县"].ToString();
                textBox1.Text = dr["居民姓名"].ToString();
                textBox2.Text = dr["性别"].ToString();
                textBox3.Text = dr["工作单位"].ToString();
                textBox4.Text = dr["居民编号"].ToString();
                textBox5.Text = dr["出生日期"].ToString();
                textBox6.Text = dr["联系电话"].ToString();
                textBox7.Text = dr["诊疗卡号"].ToString();
                textBox8.Text = dr["职业"].ToString();
                textBox9.Text = str1 + str2 + str3;
                textBox10.Text = dr["身份证号"].ToString();
                textBox11.Text = dr["文化程度"].ToString();
            }
                dr.Close();
          
            if (dr1.Read())
            {
                richTextBox1.Text=dr1["主观资料"].ToString();
                richTextBox2.Text=dr1["客观资料"].ToString();
                richTextBox3.Text = dr1["评估"].ToString();
                richTextBox4.Text = dr1["处置计划"].ToString();
                //dateTimePicker1.Text = dr1["接诊日期"].ToString();
                dateTimePicker1.Text = dr1.GetValue(4).ToString();
                textBox12.Text = dr1["医生签名"].ToString();
                
            }
            dr1.Close();

            
           
        }    

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            richTextBox1.Text = "";
            richTextBox2.Text = "";
            richTextBox3.Text = "";
            richTextBox4.Text = "";
           
           
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void toolStripButton2_Click(object sender, EventArgs e)  //保存，更新
        {   
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();

            string sql4 = "select count(inhabitantcode_chr) from T_CS_RECEIVEEXAMINERECORD where inhabitantcode_chr='" + cr_JMBH + "'"; 
            SqlCommand df4 = new SqlCommand(sql4, cn);
            SqlDataReader dr = df4.ExecuteReader();
            dr.Read();
            string d = dr.GetValue(0).ToString();
            dr.Close();
            if (d == "0")
            {
                string sql = "insert into T_CS_RECEIVEEXAMINERECORD(INHABITANTCODE_CHR,ZGZL_VCHR,KGZL_VCHR,PG_VCHR,CZJH_VCHR,CREATEDOCTORMANE_VCHR,CREATEDATE_DAT) Values(" + textBox4.Text + ",'" + richTextBox1.Text + "','" + richTextBox2.Text + "','" + richTextBox3.Text + "','" + richTextBox4.Text + "','" + textBox12.Text + "'," + dateTimePicker2.Text + ")";
                SqlCommand df = new SqlCommand(sql, cn);

                try
                {
                    df.ExecuteNonQuery();
                    MessageBox.Show("插入成功", "提示");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("不能插入数据！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
                }

            }
            else
            {
                string sql = "update T_CS_RECEIVEEXAMINERECORD set ZGZL_VCHR='" + richTextBox1.Text + "',KGZL_VCHR='" + richTextBox2.Text + "',PG_VCHR='" + richTextBox3.Text + "',CZJH_VCHR='" + richTextBox4.Text + "',CREATEDOCTORMANE_VCHR='" + textBox12.Text + "',CREATEDATE_DAT=" + dateTimePicker2.Text;
                SqlCommand dc = new SqlCommand(sql, cn);
                
                try
                {
                    dc.ExecuteNonQuery();
                    MessageBox.Show("更新成功", "提示");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("无法更新数据！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
                }
            }
            cn.Close();
        }

        private void toolStripButton3_Click(object sender, EventArgs e) //删除
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();

            string sql = "delete T_CS_RECEIVEEXAMINERECORD where inhabitantcode_chr=" + textBox4.Text;
            SqlCommand dc = new SqlCommand(sql, cn);
            try
            {
                dc.ExecuteNonQuery();
                MessageBox.Show("删除成功", "提示");
                richTextBox1.Text = "";
                richTextBox2.Text = "";
                richTextBox3.Text = "";
                richTextBox4.Text = "";
                textBox12.Text = "";

            }
            catch (SqlException ex)
            {
                MessageBox.Show("删除失败！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
            }
           
            
            
        }
    }
}
